package com.ymatou.iapi.sellercs.testcase;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.UpdateDiscourseBean;
import com.ymatou.iapi.sellercs.service.UpdateDiscourseCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*修改话术
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateDiscourse{
	private static UpdateDiscourseBean updatediscourseBean;
	private static UpdateDiscourseCall updatediscourseCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改话术");
	}

	@Before
	public void caseUp() {
		updatediscourseBean=new UpdateDiscourseBean();
		updatediscourseCall=new UpdateDiscourseCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatediscourseCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateDiscourse_001() {
		Logger.start(true,"修改话术成功");
		try {
			String title="话术分类";
			String discourse="test";
			String CategoryId=SellerCSIMCallService.getCategoryId(sellerId, title);
			String id=SellerCSIMCallService.AddDiscourse(sellerId, title, discourse);
			updatediscourseBean.setCategoryId(CategoryId);
			updatediscourseBean.setCSId(sellerId);
			updatediscourseBean.setId(id);
			updatediscourseBean.setSort(2);
			updatediscourseBean.setTitle(TestDataManager.getRandomUUID().toString());
			updatediscourseCall.setData(updatediscourseBean);
			updatediscourseCall.callService();			
			Logger.verifyIsContains("200", updatediscourseCall.getString("Status"), "验证Status");	
			Logger.verifyEquals(true, updatediscourseCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(), "验证Success");	
			List<Map>lm=YmtSellerCSIMWapper.selectIndividualdiscourseByCsid(sellerId);
			Logger.debug(id+":"+lm);
			lm = lm.stream().filter(m -> {
				if (m.get("id") != null) {
					return m.get("id").equals(id);
				} else {
					return false;
				}
			}).collect(Collectors.toList());
			Logger.verifyEquals(1, lm.size(), "查询到对应记录");
			//[{CategoryId=3abd471faa634c1bab70698e9eb0d24d, icTitle=话术类别d3863, CSId=24635724, cTitle=null, id=null, cSort=null, icSort=1}]
			Logger.verifyEquals(sellerId,lm.get(0).get("CSId"),"验证csid");
			Logger.verifyEquals(title,lm.get(0).get("icTitle"),"验证icTitle");
			Logger.verifyEquals(updatediscourseBean.getTitle(),lm.get(0).get("cTitle"),"验证cTitle");
			Logger.verifyEquals(updatediscourseBean.getSort(),lm.get(0).get("cSort"),"验证cSort");
			Logger.verifyEquals(updatediscourseBean.getCategoryId(),lm.get(0).get("CategoryId"),"验证CategoryId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateDiscourse_002() {
		Logger.start(true,"不输入csid失败");
		try {
			String title="话术分类";
			String discourse="test";
			String CategoryId=SellerCSIMCallService.getCategoryId(sellerId, title);
			String id=SellerCSIMCallService.AddDiscourse(sellerId, title, discourse);
			updatediscourseBean.setCategoryId(CategoryId);
//			updatediscourseBean.setCSId(sellerId+1);
			updatediscourseBean.setId(id);
			updatediscourseBean.setSort(2);
			updatediscourseBean.setTitle(TestDataManager.getRandomUUID().toString());
			updatediscourseCall.setData(updatediscourseBean);
			updatediscourseCall.callService();			
			Logger.verifyIsContains("400", updatediscourseCall.getString("Status"), "验证Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateDiscourse_003() {
		Logger.start(true,"不输入Id失败");
		try {
			String title="话术分类";
			String discourse="test";
			String CategoryId=SellerCSIMCallService.getCategoryId(sellerId, title);
			String id=SellerCSIMCallService.AddDiscourse(sellerId, title, discourse);
			updatediscourseBean.setCategoryId(CategoryId);
			updatediscourseBean.setCSId(sellerId+1);
//			updatediscourseBean.setId(id);
			updatediscourseBean.setSort(2);
			updatediscourseBean.setTitle(TestDataManager.getRandomUUID().toString());
			updatediscourseCall.setData(updatediscourseBean);
			updatediscourseCall.callService();			
			Logger.verifyIsContains("400", updatediscourseCall.getString("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UpdateDiscourse_004() {
		Logger.start(true,"输入csid和Id不匹配不修改");
		try {
			String title="话术分类";
			String discourse="test";
			String CategoryId=SellerCSIMCallService.getCategoryId(sellerId, title);
			String id=SellerCSIMCallService.AddDiscourse(sellerId, title, discourse);
			updatediscourseBean.setCategoryId(CategoryId);
			updatediscourseBean.setCSId(sellerId+1);
			updatediscourseBean.setId(id);
			updatediscourseBean.setSort(2);
			updatediscourseBean.setTitle(TestDataManager.getRandomUUID().toString());
			updatediscourseCall.setData(updatediscourseBean);
			updatediscourseCall.callService();			
			Logger.verifyIsContains("200", updatediscourseCall.getString("Status"), "验证Status");	
			Logger.verifyEquals(false, updatediscourseCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(), "验证Success");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}